home *** CD-ROM | disk | FTP | other *** search
- // Acronymity
- // © 2000 by Karl Kornel (kornel.1@osu.edu)
- // Made during MacHack 2000
- // Changes & Redistribution allowed is credit given
-
- /* The headers we need */
- #include <iostream.h>
- #include <fstream.h>
- #include <unistd.h>
- #include <stdlib.h>
- #include <time.h>
-
- /* The User-Defined Settings */
- #include "settings.h"
-
- /* The prototypes for the functions in this file */
- bool openFiles(void);
- int getWord(char *, const char);
- void getTheWord(char *, const char, const int);
- void openError(const char);
- void moveToStart(void);
- void closeFiles(void);
-
- /* Prototypes for any non-local functions */
- int getLetterCode(const char);
-
- /* Global variables */
- int lineUsed[26] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
-
- /* The file streams */
- ifstream index;
- ifstream a;
- ifstream b;
- ifstream c;
- ifstream d;
- ifstream e;
- ifstream f;
- ifstream g;
- ifstream h;
- ifstream i;
- ifstream j;
- ifstream k;
- ifstream l;
- ifstream m;
- ifstream n;
- ifstream o;
- ifstream p;
- ifstream q;
- ifstream r;
- ifstream s;
- ifstream t;
- ifstream u;
- ifstream v;
- ifstream w;
- ifstream x;
- ifstream y;
- ifstream z;
-
- /* The code */
- bool openFiles(void) { // Open out files
- bool openStatus = true; // Assume that all is well
-
- index.open("index.dat",ios::in);
- if (!index) openError('\0');
- a.open("a.txt",ios::in);
- if (!a) openError('a');
- b.open("b.txt",ios::in);
- if (!b) openError('b');
- c.open("c.txt",ios::in);
- if (!c) openError('c');
- d.open("d.txt",ios::in);
- if (!d) openError('d');
- e.open("e.txt",ios::in);
- if (!e) openError('e');
- f.open("f.txt",ios::in);
- if (!f) openError('f');
- g.open("g.txt",ios::in);
- if (!g) openError('g');
- h.open("h.txt",ios::in);
- if (!h) openError('h');
- i.open("i.txt",ios::in);
- if (!i) openError('i');
- j.open("j.txt",ios::in);
- if (!j) openError('j');
- k.open("k.txt",ios::in);
- if (!k) openError('k');
- l.open("l.txt",ios::in);
- if (!l) openError('l');
- m.open("m.txt",ios::in);
- if (!m) openError('m');
- n.open("n.txt",ios::in);
- if (!n) openError('n');
- o.open("o.txt",ios::in);
- if (!o) openError('o');
- p.open("p.txt",ios::in);
- if (!p) openError('p');
- q.open("q.txt",ios::in);
- if (!q) openError('q');
- r.open("r.txt",ios::in);
- if (!r) openError('r');
- s.open("s.txt",ios::in);
- if (!s) openError('s');
- t.open("t.txt",ios::in);
- if (!t) openError('t');
- u.open("u.txt",ios::in);
- if (!u) openError('u');
- v.open("v.txt",ios::in);
- if (!v) openError('v');
- w.open("w.txt",ios::in);
- if (!w) openError('w');
- x.open("x.txt",ios::in);
- if (!x) openError('x');
- y.open("y.txt",ios::in);
- if (!y) openError('y');
- z.open("z.txt",ios::in);
- if (!z) openError('z');
-
- return openStatus;
- }
-
- int getWord(char *wordSpace, const char letter) { // Get a word from a file
- /* Variables */
- char letterComma = '\0'; // One character for the letter, and one comma
- int letterEntries; // The # of entries in the line (for a letter)
- int chosenLine = 1; // The line from which we will pull text
- int theWordLength = 0; // The length of the word
- int whereAreWeNow = 1; // The character of the word we're looking at now
-
- /* Keep searching until we find the correct letter (and it's # of words) */
- while (letterComma != letter) // As long as we can't find the right letter
- index >> letterComma >> letterEntries; // Pull in another line
-
- /* Now, let's pull the random word from the correct file */
- getTheWord(wordSpace,letter,letterEntries);
-
- /* Now, let's find out the word's length */
- bool continueSearching = true; // Keep looking
- while (continueSearching == true) {
- if (wordSpace[whereAreWeNow - 1] != '\0')
- whereAreWeNow = whereAreWeNow + 1; // Move forward one character
- else
- continueSearching = false; // We can stop searching
- }
-
- /* Go back to the beginning */
- moveToStart();
-
- return whereAreWeNow - 1; // Return the length of the word (minux the /0)
- }
-
- void getTheWord(char *wordSpace,const char letter, const int letterEntries) {
- int chosenLine = 1;
-
- srand(time(NULL)); // Seed the random-number system
- while (true) { // Repeat forever (until we break out)
- chosenLine = 1 + (rand() % letterEntries); // Choose the line
- if (chosenLine != lineUsed[getLetterCode(letter)]) { // If it doesn't match
- lineUsed[getLetterCode(letter)] = chosenLine;
- break; // Break out of the loop
- } else if (letterEntries == 1) // If there's only one word for this letter
- break; // Break out of the loop
- }
-
- switch (letter) {
- case 'a':
- for (int temp=1;temp < chosenLine;temp++)
- a.ignore(wordLength + 1, '\n'); // Ignore the line
- a >> wordSpace; // Pull in the line text
- break; // Get out of the structure
- case 'b':
- for (int temp=1;temp < chosenLine;temp++)
- b.ignore(wordLength + 1, '\n'); // Ignore the line
- b >> wordSpace;
- break; // Get out of the structure
- case 'c':
- for (int temp=1;temp < chosenLine;temp++)
- c.ignore(wordLength + 1, '\n'); // Ignore the line
- c >> wordSpace;
- break; // Get out of the structure
- case 'd':
- for (int temp=1;temp < chosenLine;temp++)
- d.ignore(wordLength + 1, '\n'); // Ignore the line
- d >> wordSpace;
- break; // Get out of the structure
- case 'e':
- for (int temp=1;temp < chosenLine;temp++)
- e.ignore(wordLength + 1, '\n'); // Ignore the line
- e >> wordSpace;
- break; // Get out of the structure
- case 'f':
- for (int temp=1;temp < chosenLine;temp++)
- f.ignore(wordLength + 1, '\n'); // Ignore the line
- f >> wordSpace;
- break; // Get out of the structure
- case 'g':
- for (int temp=1;temp < chosenLine;temp++)
- g.ignore(wordLength + 1, '\n'); // Ignore the line
- g >> wordSpace;
- break; // Get out of the structure
- case 'h':
- for (int temp=1;temp < chosenLine;temp++)
- h.ignore(wordLength + 1, '\n'); // Ignore the line
- h >> wordSpace;
- break; // Get out of the structure
- case 'i':
- for (int temp=1;temp < chosenLine;temp++)
- i.ignore(wordLength + 1, '\n'); // Ignore the line
- i >> wordSpace;
- break; // Get out of the structure
- case 'j':
- for (int temp=1;temp < chosenLine;temp++)
- j.ignore(wordLength + 1, '\n'); // Ignore the line
- j >> wordSpace;
- break; // Get out of the structure
- case 'k':
- for (int temp=1;temp < chosenLine;temp++)
- k.ignore(wordLength + 1, '\n'); // Ignore the line
- k >> wordSpace;
- break; // Get out of the structure
- case 'l':
- for (int temp=1;temp < chosenLine;temp++)
- l.ignore(wordLength + 1, '\n'); // Ignore the line
- l >> wordSpace;
- break; // Get out of the structure
- case 'm':
- for (int temp=1;temp < chosenLine;temp++)
- m.ignore(wordLength + 1, '\n'); // Ignore the line
- m >> wordSpace;
- break; // Get out of the structure
- case 'n':
- for (int temp=1;temp < chosenLine;temp++)
- n.ignore(wordLength + 1, '\n'); // Ignore the line
- n >> wordSpace;
- break; // Get out of the structure
- case 'o':
- for (int temp=1;temp < chosenLine;temp++)
- o.ignore(wordLength + 1, '\n'); // Ignore the line
- o >> wordSpace;
- break; // Get out of the structure
- case 'p':
- for (int temp=1;temp < chosenLine;temp++)
- p.ignore(wordLength + 1, '\n'); // Ignore the line
- p >> wordSpace;
- break; // Get out of the structure
- case 'q':
- for (int temp=1;temp < chosenLine;temp++)
- q.ignore(wordLength + 1, '\n'); // Ignore the line
- q >> wordSpace;
- break; // Get out of the structure
- case 'r':
- for (int temp=1;temp < chosenLine;temp++)
- r.ignore(wordLength + 1, '\n'); // Ignore the line
- r >> wordSpace;
- break; // Get out of the structure
- case 's':
- for (int temp=1;temp < chosenLine;temp++)
- s.ignore(wordLength + 1, '\n'); // Ignore the line
- s >> wordSpace;
- break; // Get out of the structure
- case 't':
- for (int temp=1;temp < chosenLine;temp++)
- t.ignore(wordLength + 1, '\n'); // Ignore the line
- t >> wordSpace;
- break; // Get out of the structure
- case 'u':
- for (int temp=1;temp < chosenLine;temp++)
- u.ignore(wordLength + 1, '\n'); // Ignore the line
- u >> wordSpace;
- break; // Get out of the structure
- case 'v':
- for (int temp=1;temp < chosenLine;temp++)
- v.ignore(wordLength + 1, '\n'); // Ignore the line
- v >> wordSpace;
- break; // Get out of the structure
- case 'w':
- for (int temp=1;temp < chosenLine;temp++)
- w.ignore(wordLength + 1, '\n'); // Ignore the line
- w >> wordSpace;
- break; // Get out of the structure
- case 'x':
- for (int temp=1;temp < chosenLine;temp++)
- x.ignore(wordLength + 1, '\n'); // Ignore the line
- x >> wordSpace;
- break; // Get out of the structure
- case 'y':
- for (int temp=1;temp < chosenLine;temp++)
- y.ignore(wordLength + 1, '\n'); // Ignore the line
- y >> wordSpace;
- break; // Get out of the structure
- case 'z':
- for (int temp=1;temp < chosenLine;temp++)
- z.ignore(wordLength + 1, '\n'); // Ignore the line
- z >> wordSpace;
- break; // Get out of the structure
- default:
- for (int temp=1;temp <= 29;temp++)
- cout << '\n';
- cout << endl << "ERROR! Unable to find correct file." << endl;
- exit(-37);
- }
- }
-
- void openError(const char letter) { // Handle file-open errors
- for (int temp = 1;temp <= 29;temp++)
- cout << "\n";
- cout << endl;
-
- cout << "ERROR!\n\n"
- << "The file " << letter << ".txt does not exist, please make one." << endl;
-
- sleep(1);
-
- exit(-130);
- }
-
- void moveToStart(void) {
- index.seekg(0,ios::beg);
- a.seekg(0,ios::beg);
- b.seekg(0,ios::beg);
- c.seekg(0,ios::beg);
- d.seekg(0,ios::beg);
- e.seekg(0,ios::beg);
- f.seekg(0,ios::beg);
- g.seekg(0,ios::beg);
- h.seekg(0,ios::beg);
- i.seekg(0,ios::beg);
- j.seekg(0,ios::beg);
- k.seekg(0,ios::beg);
- l.seekg(0,ios::beg);
- m.seekg(0,ios::beg);
- n.seekg(0,ios::beg);
- o.seekg(0,ios::beg);
- p.seekg(0,ios::beg);
- q.seekg(0,ios::beg);
- r.seekg(0,ios::beg);
- s.seekg(0,ios::beg);
- t.seekg(0,ios::beg);
- u.seekg(0,ios::beg);
- v.seekg(0,ios::beg);
- w.seekg(0,ios::beg);
- x.seekg(0,ios::beg);
- y.seekg(0,ios::beg);
- z.seekg(0,ios::beg);
- }
-
- void closeFiles(void) { // Close our files
- index.close();
- a.close();
- b.close();
- c.close();
- d.close();
- e.close();
- f.close();
- g.close();
- h.close();
- i.close();
- j.close();
- k.close();
- l.close();
- m.close();
- n.close();
- o.close();
- p.close();
- q.close();
- r.close();
- s.close();
- t.close();
- u.close();
- v.close();
- w.close();
- x.close();
- y.close();
- z.close();
- }